<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        :root {
            --primary: #00f7ff;
            --secondary: #0066ff;
            --dark: #0a0e17;
            --light: #e0f7fa;
            --glow: 0 0 10px rgba(0, 247, 255, 0.7);
        }
        
        body {
            font-family: 'Microsoft YaHei', 'Segoe UI', sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            color: var(--light);
            background-color: var(--dark);
            background-image: 
                radial-gradient(circle at 10% 20%, rgba(0, 103, 255, 0.1) 0%, transparent 20%),
                radial-gradient(circle at 90% 80%, rgba(0, 247, 255, 0.1) 0%, transparent 20%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .login-container {
            background: linear-gradient(145deg, rgba(16, 22, 36, 0.8) 0%, rgba(0, 40, 80, 0.6) 100%);
            border-radius: 16px;
            padding: 40px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            border: 1px solid rgba(0, 247, 255, 0.1);
            width: 100%;
            max-width: 400px;
            position: relative;
        }
        
        .login-container::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 4px;
            background: linear-gradient(to right, var(--primary), var(--secondary));
        }
        
        h1 {
            text-align: center;
            margin-bottom: 30px;
            font-size: 2em;
            background: linear-gradient(to right, var(--primary), var(--secondary));
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            color: var(--primary);
            font-weight: bold;
        }
        
        input {
            width: 100%;
            padding: 12px;
            border: 1px solid rgba(0, 247, 255, 0.3);
            border-radius: 8px;
            background: rgba(10, 14, 23, 0.5);
            color: var(--light);
            font-size: 16px;
            box-sizing: border-box;
            transition: all 0.3s ease;
        }
        
        input:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 10px rgba(0, 247, 255, 0.3);
        }
        
        .btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(to right, var(--primary), var(--secondary));
            color: var(--dark);
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 15px;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 247, 255, 0.4);
        }
        
        .btn-secondary {
            background: transparent;
            border: 1px solid var(--primary);
            color: var(--primary);
        }
        
        .btn-secondary:hover {
            background: var(--primary);
            color: var(--dark);
        }
        
        .error {
            color: #ff6b6b;
            font-size: 14px;
            margin-top: 5px;
        }
        
        .success {
            color: #51cf66;
            font-size: 14px;
            margin-top: 5px;
        }
        
        .admin-info {
            background: rgba(255, 193, 7, 0.1);
            border: 1px solid rgba(255, 193, 7, 0.3);
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 20px;
            font-size: 14px;
        }
        
        .admin-info h4 {
            margin: 0 0 10px 0;
            color: #ffc107;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h1>用户登录</h1> 
        <div class="admin-info">
            <h4>管理员登录：</h4>
            <p>用户名：admin<br>密码：666666</p>
        </div>
        
        <form id="loginForm">
            <!--@IndexLoginFormBegin-->
             
            <div class="form-group">
                <label for="username">手机号</label>
                <input type="text" id="username" name="username" placeholder="请输入手机号" required>
                <div class="error" id="usernameError"></div>
            </div> 
           
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码" required> 
                 
                <div class="error" id="passwordError"></div>
            </div>
            
            <button type="submit" class="btn">登录</button>
            <button type="button" class="btn btn-secondary" onclick="window.location.href='register.html'">没有账户？去注册</button>
        <!--@IndexLoginFormEnd-->
        </form>
        
        <div class="success" id="successMessage"></div>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault(); 
            // 清除之前的错误信息
            clearErrors();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            if (!username || !password) {
                showError('usernameError', '请填写完整信息');
                return;
            }
            
			console.log('Before fetch');  // 确保这行输出
            try {
                const response = await fetch('login', {
					method: 'POST',
					credentials: 'include', // 保持现有
					headers: {
						'Content-Type': 'application/json',
						'Accept': 'application/json' // 添加Accept头
					},
					body: JSON.stringify({ username, password }),
					mode: 'cors' // 明确指定CORS模式
				});
                
                const data = await response.json();  
                if (response.ok && data.code === 1) {
                    document.getElementById('successMessage').textContent = '登录成功！正在跳转...';
                    
                    // 根据用户角色跳转到不同页面
                    setTimeout(() => {
                        // if (data.user.role === 'admin') {
                        //     window.location.href = 'admin.html';
                        // } else {
                        //     window.location.href = 'qianrushi.html';
                        // }
                        window.location.href = 'qianrushi.html';
                    }, 1500);
                } else {
                    showError('usernameError', data.error);
                }
				 console.log('Fetch completed', response);  // 检查是否执行到这里
            } catch (error) {
				console.error('Fetch failed:', error);
				let errorMsg = '登录失败，请稍后重试';
				if (error instanceof TypeError) {
					errorMsg = '网络错误，请检查连接';
				}
				showError('usernameError', errorMsg);
            }
        });
        
        function showError(elementId, message) {
            document.getElementById(elementId).textContent = message;
        }
        
        function clearErrors() {
            document.getElementById('usernameError').textContent = '';
            document.getElementById('passwordError').textContent = '';
            document.getElementById('successMessage').textContent = '';
        }
    </script>
</body>
</html> 